﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ProAsync
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Main方法开始");


            /*Task 异步*/
            //Task<string> task = GetStrAsync();

            //for (int i = 0; i < 5; i++)
            //{
            //    Console.WriteLine("Main方法过程 " + i.ToString());
            //}
            //Console.WriteLine("Main方法结束 ============");
            //Console.WriteLine("等待异步返回结果>>>");
            ////Task.WaitAll(task);
            //Console.WriteLine("异步返回结果:" + task.Result);
            /*Task 异步*/

            /*BeginInvoke 实现异步*/
            Func<bool> FuncAction = new Func<bool>(WriteTxt);
            FuncAction.BeginInvoke(null, null);
            /*BeginInvoke 实现异步 参考:www.cr173.com/html/17923_1.html */

            Console.WriteLine("Main方法结束");


            Console.ReadKey();
        }



        public static async Task<string> GetStrAsync()
        {

            return await Task.Factory.StartNew(() =>
              {
                  Thread.Sleep(3000);//模拟异步执行时间
                  Console.WriteLine("异步输出。。。。");
                  return "这是异步返回结果";
              });
        }

        public static bool WriteTxt()
        {
            for (int i = 0; i < 150; i++)
            {
                Console.WriteLine(i);
            }
            return true;
        }



    }
}
